<!DOCTYPE html>
<html>
<head>
	<?php
	include "connection-provider.php";
	include "functions.php";
?>
</head>
<body>
<?php
	$questionTable = "question_table";
	$questionNumber = $_POST['hiddenQuestionId'];
	
			/*
			*
			*	For adding New questions
			*
			*	$_POST['questionSubmitButton'] -- from add-questions.php
			*
			*/
	
	
			if(isset($_POST['questionSubmitButton']))
				{
				
			
			$question = mysql_real_escape_string($_POST['questionTextArea']);			
			$optionOne = mysql_real_escape_string($_POST['optionOne']);
			$optionTwo = mysql_real_escape_string($_POST['optionTwo']);
			$optionThree = mysql_real_escape_string($_POST['optionThree']);
			$optionFour = mysql_real_escape_string($_POST['optionFour']);
			
						
			$questionType = $_POST['hiddenId'];   //$_POST['questionTypeId']; ------------have to change
			
		// Validating questions ---- functions.php
		validateQuestion($questionTable, $question, $optionOne, $optionTwo, $optionThree, $optionFour, $_POST['optionRadio'], $questionNumber);
		
			if($_POST['optionRadio']=="optionOne")
				{
					$correctAnswer = $optionOne;
				}
			elseif($_POST['optionRadio']=="optionTwo")
				{
					$correctAnswer = $optionTwo;
				}
			elseif($_POST['optionRadio']=="optionThree")
				{
					$correctAnswer = $optionThree;
				}
			elseif($_POST['optionRadio']=="optionFour")
				{
					$correctAnswer = $optionFour;
				}	

				
				
				$sqlQuestion = "insert into `$questionTable`(typeId, question, correctAnswer, optionOne, optionTwo, optionThree, optionFour) values('$questionType', '$question', '$correctAnswer', '$optionOne', '$optionTwo', '$optionThree', '$optionFour')";
				if(mysqli_query($con, $sqlQuestion))
					{
						$url="add-question.php?isAdded=yes&selectType=$questionType";
						header("Refresh:0;URL=$url");
					}
					else
					{
						$url="add-question.php?isAdded=no";
						header("Refresh:0;URL=$url");
					}
				}
		

			/*
			*
			*	For Updating questions
			*
			*	$_POST['questionUpdateButton'] -- from add-questions.php
			*
			*/		
				
			elseif(isset($_POST['questionUpdateButton']))
				{
				
			$questionTable = "question_table";
			$question = mysql_real_escape_string($_POST['questionTextArea']);			
			$optionOne = mysql_real_escape_string($_POST['optionOne']);
			$optionTwo = mysql_real_escape_string($_POST['optionTwo']);
			$optionThree = mysql_real_escape_string($_POST['optionThree']);
			$optionFour = mysql_real_escape_string($_POST['optionFour']);
			
			$questionType = $_POST['hiddenId'];
			
			
			//$questionType = 1;   //$_POST['questionTypeId']; ------------have to change
	
		// validating question form ----- functions.php
	validateQuestion($questionTable, $question, $optionOne, $optionTwo, $optionThree, $optionFour, $_POST['optionRadio'], $questionNumber);
		

		
			if($_POST['optionRadio']=="optionOne")
				{
					$correctAnswer = $optionOne;
				}
			elseif($_POST['optionRadio']=="optionTwo")
				{
					$correctAnswer = $optionTwo;
				}
			elseif($_POST['optionRadio']=="optionThree")
				{
					$correctAnswer = $optionThree;
				}
			elseif($_POST['optionRadio']=="optionFour")
				{
					$correctAnswer = $optionFour;
				}	
			
			$questionNumber = $_POST['hiddenQuestionId'];
				echo $questionNumber;
				echo $question;
				echo $correctAnswer . $optionOne  . $optionTwo . $optionThree . $optionFour;
				//exit(0);
				
				
				//$q = "UPDATE `$questionTable` SET typeId=343 WHERE questionNumber=$questionNumber";
				
				
					$q = "UPDATE `$questionTable` SET typeId='$questionType', question='$question', correctAnswer='$correctAnswer', optionOne='$optionOne', optionTwo='$optionTwo', optionThree='$optionThree', optionFour='$optionFour'  WHERE questionNumber=$questionNumber";
					
					
					//$q = "UPDATE `$tableName` SET image_name='$filename', page='$content' WHERE id='$noOfPages'";
				   
				   if(mysqli_query($con, $q))
					{
						//echo $questionNumber;
						//exit(0);
						header( "Location: add-question.php?isUpdated=yes&selectType=$questionType" );
					}

					else
					{
						header( "Location: add-question.php?isUpdated=no" );
					}
				}
				elseif(isset($_GET['deleteQuestionId']) && isset($_GET['deleteQuestionId']))
				{
					if(mysql_query("DELETE FROM `$questionTable` WHERE questionNumber = '$_GET[deleteQuestionId]'"))
						{
							$questionType = $_GET['selectType'];
							$url="add-question.php?isDeleted=yes&selectType=$questionType";					
							header("Refresh:0;URL=$url");
						}
				}
				
				//$questionTable = "question_table";
		//}

?>

</body>
</html>